<?php
header("content-type:text/html;charset=utf8");
$pdo = new PDO("mysql:host=127.0.0.1;dbname=11-9;charset=utf8",'root','root');
$redis =new Redis();
$redis->connect("127.0.0.1",'6379');
//当前页
$page = isset($_GET['page'])?$_GET['page']:1;
//总条数
$sql = "select count(*) from exe";
$res=$pdo->query($sql)->fetch();
$pagenum = $res['count(*)'];
//每页条数
$pagesize = 3;
//总页数
$totalpage = ceil($pagenum/$pagesize);
//偏移量
$limit = ($page-1)*$pagesize;
//如果redis中存在数据，则直接从redis中读取，如果没有，侧从数据库取
if($redis->get("data".$page))
{
//    echo "这是redis数据";
    $data = $redis->get("data".$page);
    $data = json_decode($data,true);
}else{
    $sql = "select * from exe limit $limit,$pagesize";
    $data = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    $redisdata = json_encode($data);
    $redis ->set("data".$page,$redisdata);
}



$pre = $page-1>0 ?$page-1:1;
$next = $page+1>$totalpage?$totalpage:$page+1;
$str ="  <a href='show.php?page=1'>首页</a> <a href='show.php?page=$pre'>上一页</a> <a href='show.php?page=$next'>下一页</a> <a href='show.php?page=$totalpage'>尾页</a> "

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<center>
    <table>
        <tr>

            <td>商品名</td>
            <td>价格</td>
            <td>数量</td>
            <td>操作</td>
        </tr>
        <?php foreach ($data as $val) {?>
        <tr>
            <td><?php echo $val['name']?></td>
            <td><?php echo $val['money']?></td>
            <td><?php echo $val['age']?></td>
            <td><a href="detail.php?id=<?php echo $val['id'] ?>"><input type="button" value="详情"></a></td>

        </tr>
        <?php }?>
    </table>
    <?php echo $str;?>
</center>
</body>
</html>
